using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

using FreeFlow.Administration;
using FreeFlow.WinFormsControls.HelperClasses;

namespace FreeFlowAdministrator
{
	/// <summary>
	/// Summary description for FindFolderControl.
	/// </summary>
	public class FindFolderControl : System.Windows.Forms.UserControl
	{
    private System.Windows.Forms.Panel panel1;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.TextBox findTextBox;
    private System.Windows.Forms.Panel resultPanel;
    private FreeFlowAdministrator.UIActionsControl uiActionsControl;
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

    public FindFolderControl()
    {
      // This call is required by the Windows.Forms Form Designer.
      InitializeComponent();

      ShowActions();
    }

    private void ShowActions()
    {
      UIActionList actionList = new UIActionList();
      actionList.Add("Find Folder", "Find any folders that match the search text", 
        new ActionExecuteDelegate(FindFolder));
		  
      if (resultPanel.Controls.Count > 0)
      {
        if (resultPanel.Controls[0] is FolderControl)
        {
          FolderControl folderControl = (FolderControl)resultPanel.Controls[0];
          actionList.Add("Folder");
          actionList.Add(folderControl.ActionList);
        }
        else if (resultPanel.Controls[0] is FoldersControl)
        {
          FoldersControl foldersControl = (FoldersControl)resultPanel.Controls[0];
          actionList.Add("Folders");
          actionList.Add(foldersControl.ActionList);
        }
      }
      
      uiActionsControl.Actions = actionList;
    }

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
      this.panel1 = new System.Windows.Forms.Panel();
      this.findTextBox = new System.Windows.Forms.TextBox();
      this.label1 = new System.Windows.Forms.Label();
      this.uiActionsControl = new FreeFlowAdministrator.UIActionsControl();
      this.resultPanel = new System.Windows.Forms.Panel();
      this.panel1.SuspendLayout();
      this.SuspendLayout();
      // 
      // panel1
      // 
      this.panel1.Controls.Add(this.findTextBox);
      this.panel1.Controls.Add(this.label1);
      this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
      this.panel1.Location = new System.Drawing.Point(0, 0);
      this.panel1.Name = "panel1";
      this.panel1.Size = new System.Drawing.Size(674, 80);
      this.panel1.TabIndex = 0;
      // 
      // findTextBox
      // 
      this.findTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                  | System.Windows.Forms.AnchorStyles.Right)));
      this.findTextBox.Location = new System.Drawing.Point(8, 48);
      this.findTextBox.Name = "findTextBox";
      this.findTextBox.Size = new System.Drawing.Size(656, 20);
      this.findTextBox.TabIndex = 1;
      // 
      // label1
      // 
      this.label1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                  | System.Windows.Forms.AnchorStyles.Right)));
      this.label1.Location = new System.Drawing.Point(8, 8);
      this.label1.Name = "label1";
      this.label1.Size = new System.Drawing.Size(658, 32);
      this.label1.TabIndex = 0;
      this.label1.Text = "Enter a folder name, folder ID or the subject to search for. Wildcards may be use" +
          "d, eg \'%\' for any number of characters, and \'_\' for any one character";
      // 
      // uiActionsControl
      // 
      this.uiActionsControl.Actions = null;
      this.uiActionsControl.Dock = System.Windows.Forms.DockStyle.Right;
      this.uiActionsControl.Location = new System.Drawing.Point(674, 0);
      this.uiActionsControl.Name = "uiActionsControl";
      this.uiActionsControl.Size = new System.Drawing.Size(150, 392);
      this.uiActionsControl.TabIndex = 2;
      // 
      // resultPanel
      // 
      this.resultPanel.Dock = System.Windows.Forms.DockStyle.Fill;
      this.resultPanel.Location = new System.Drawing.Point(0, 80);
      this.resultPanel.Name = "resultPanel";
      this.resultPanel.Size = new System.Drawing.Size(674, 312);
      this.resultPanel.TabIndex = 3;
      // 
      // FindFolderControl
      // 
      this.Controls.Add(this.resultPanel);
      this.Controls.Add(this.panel1);
      this.Controls.Add(this.uiActionsControl);
      this.Name = "FindFolderControl";
      this.Size = new System.Drawing.Size(824, 392);
      this.panel1.ResumeLayout(false);
      this.panel1.PerformLayout();
      this.ResumeLayout(false);

    }
		#endregion

    private Server server;
    public void LoadServer(Server server)
    {
      this.server = server;
    }

    private string FindFolder()
    {
      using (new CursorHandler())
      {
        FolderCollection folders = new FolderCollection(
          string.Format("eFolderID LIKE '{0}' OR eFolderName LIKE '{0}' OR eSubject LIKE '{0}'", 
          DatabaseAware.SqlEscape(findTextBox.Text)), server);
        resultPanel.Controls.Clear();
        if (folders.Count == 0)
        {
          Label label = new Label();
          label.Dock = DockStyle.Fill;
          label.TextAlign = ContentAlignment.MiddleCenter;
          label.Text = "No folders were found matching the search criteria";
          resultPanel.Controls.Add(label);
        }
        if (folders.Count == 1)
        {
          FolderControl folderControl = new FolderControl();
          folderControl.ActionsVisible = false;
          folderControl.Dock = DockStyle.Fill;
          folderControl.LoadFolder(folders[0]);
          folderControl.ActionsChanged +=new EventHandler(folderControl_ActionsChanged);
          folderControl.ItemDeleted += new EventHandler(folderControl_ItemDeleted);
          resultPanel.Controls.Add(folderControl);
        }
        else if (folders.Count > 1)
        {
          FoldersControl foldersControl = new FoldersControl();
          foldersControl.ActionsVisible = false;
          foldersControl.Dock = DockStyle.Fill;
          foldersControl.LoadFolders(folders);
          resultPanel.Controls.Add(foldersControl);
        }

        ShowActions();
      }
      return null;
    }

    void folderControl_ItemDeleted(object sender, EventArgs e)
    {
      resultPanel.Controls.Clear();
      ShowActions();
    }

    private void folderControl_ActionsChanged(object sender, EventArgs e)
    {
      ShowActions();
    }
  }
}
